package com.mijie.homi.service.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.homi.dao.commom.ReplyDao;
import com.mijie.homi.dao.commom.ReviewDao;
import com.mijie.homi.domain.commom.Reply;
import com.mijie.homi.listener.ReplyListener;
import com.mijie.homi.service.common.CommentService.COMMENT;

@Service
public class ReplyService {

	@Autowired
	private ReplyDao dao;
	@Autowired
	private ReviewDao reviewDao;
	@Autowired
	private CommentService commentService;
	@Autowired
	private ReplyListener listener;
	
	private Logger logger = Logger.getLogger(ReplyService.class);
	
	public static enum REPLY{
		MOOD_COMMENT,PHOTO_COMMENT,TOPIC_OPINION_COMMENT
	}
	
	public static Map<REPLY,String> replyTableMap ;
	public static Map<COMMENT,String> tableMap ;
	
	static{
		replyTableMap = new HashMap<REPLY,String>();
		replyTableMap.put(REPLY.MOOD_COMMENT,"moodCommentReply");
		replyTableMap.put(REPLY.PHOTO_COMMENT,"photoCommentReply");
		replyTableMap.put(REPLY.TOPIC_OPINION_COMMENT,"topicOpinionCommentReply");
		
		tableMap = new HashMap<COMMENT,String>();
		tableMap.put(COMMENT.MOOD,"moodCommentReply");
		tableMap.put(COMMENT.PHOTO,"photoCommentReply");
		tableMap.put(COMMENT.TOPIC_OPINION,"topicOpinionCommentReply");
	}
	
	public void postReply(Enum<REPLY> type,Reply reply){
		reply.setIdx(Long.MAX_VALUE-System.currentTimeMillis());
		reply.setTableName(replyTableMap.get(type));
		if(dao.postReply(reply)>0){
			commentService.increNumOfReply(type, reply.getTargetId());
		}
	}
	
	public void deleteReply(Enum<REPLY> type,int sender,long id,long parentId){
		dao.deleteReply(replyTableMap.get(type), sender, id);
		listener.deleteReply(type, parentId);
	}
	
	public void deleteReplyByAdmin(Enum<REPLY> type,int administrator,long id,long parentId){
		dao.deleteReplyByAdmin(replyTableMap.get(type), administrator, id);
		listener.deleteReply(type, parentId);
	}
	
	public List<Reply> listReply(Enum<REPLY> type,long idx,long targetId,int limit){
		return dao.listReply(replyTableMap.get(type), idx,targetId, limit);
	}
	
	public Reply getReplyById(Enum<REPLY> type,long id){
		return dao.getReplyById(replyTableMap.get(type), id);
	}
	
	public List<Reply> listReplyByTargetIds(Enum<COMMENT> type,long targetId,int limit){
		return dao.listReplyByTargetIds(tableMap.get(type), targetId, limit);
	}
}
